package com.qf.studentmanager.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.qf.studentmanager.model.Grade;
import com.qf.studentmanager.model.RespBean;
import com.qf.studentmanager.service.GradeService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(urlPatterns = "/Grade")
public class GradeServlet extends HttpServlet {
    GradeService gradeService = new GradeService();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ObjectMapper objectMapper = new ObjectMapper();

        RespBean respBean =null;
        response.setContentType("application/json;charset=utf-8");
        String gradeName = request.getParameter("gradeName");
        String[] cids = request.getParameterValues("cids");
        Integer integer = gradeService.addGrade(gradeName, cids);
        if (integer == 1){
            respBean = RespBean.ok("添加成功");
        }else if (integer == -2){
            respBean = RespBean.error("添加失败，年纪重复");
        }
        String string = objectMapper.writeValueAsString(respBean);
        response.getWriter().write(string);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        response.setContentType("application/json;charset=utf-8");
        String action = request.getParameter("action");
        List<Grade> grades = null;
        if ("page".equals(action)){
            request.getRequestDispatcher("WEB-INF/jsp/grade/gradeList.jsp").forward(request, response);
        }else if ("data".equals(action)){
             grades = gradeService.getAllGrade();
            String string = objectMapper.writeValueAsString(grades);
            response.getWriter().write(string);
        }else if("from-clazz".equals(action)){
            grades = gradeService.getAllGrade();
            Grade grade = new Grade();//建一个年级叫“全部”
            grade.setGradeName("全部");
            grades.add(grade);
            String string = objectMapper.writeValueAsString(grades);
            response.getWriter().write(string);
        }
    }

    @Override
    protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        resp.setContentType("application/json;charset=utf-8");
        String gid = req.getParameter("gid");
        Integer integer = gradeService.deleteGradeByGid(Integer.parseInt(gid));
        RespBean respBean = null;
        if (integer== 1){
            respBean = RespBean.ok("删除成功");
        }else {
            respBean= RespBean.error("删除失败");
        }
        String string = objectMapper.writeValueAsString(respBean);
        resp.getWriter().write(string);

    }
}
